package com.zuxia.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.zuxia.dao.IAdminDao;
import com.zuxia.entity.Admin;

@Repository("adminDao")
public class AdminDao implements IAdminDao{
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	
	/**
	 * 鐧诲綍
	 */
	@SuppressWarnings("unchecked")
	public List<Admin> login(String name,String pwd) {
		return (List<Admin>) hibernateTemplate.find("FROM Admin where admin_name=? and admin_pwd=?",name,pwd);
	}
	/**
	 * 鏌ヨ鍏ㄩ儴
	 */
	@SuppressWarnings("unchecked")
	public List<Admin> selectAll() {
		return (List<Admin>) hibernateTemplate.find("FROM Admin");
	}
	
	/**
	 * 鏍规嵁ID鏌ヨ
	 */
	public Admin selectId(int id) {
		return (Admin) hibernateTemplate.get(Admin.class, id);
	}
	/**
	 * 鍒犻櫎
	 */
	public int deleteAdmin(Admin admin) {
		try {
			Admin a = hibernateTemplate.get(admin.getClass(), admin.getAdmin_id());
			hibernateTemplate.delete(a);
		} catch (Exception e) {
			System.out.println("鍒犻櫎澶辫触锛侊紒锛�");
			return 0;
		}
		return 1;
	}
	/**
	 * 娣诲姞绠＄悊鍛�
	 */
	public int saveAdmin(Admin admin) {
		int i = 0;
		try {
			i = (int) hibernateTemplate.save(admin);
		} catch (Exception e) {
			System.out.println("娣诲姞澶辫触锛侊紒锛�");
			return i;
		}
		return i;
	}
	
	public void updateAdmin(Admin admin) {
		Admin a = hibernateTemplate.get(Admin.class,admin.getAdmin_id());
		a.setAdmin_name(admin.getAdmin_name());
		a.setAdmin_pwd(admin.getAdmin_pwd());
		a.setAdmin_phone(admin.getAdmin_phone());
		a.setAdmin_qq(admin.getAdmin_qq());
		a.setAdmin_time(admin.getAdmin_time());
		a.setAdmin_remark(admin.getAdmin_remark());
		hibernateTemplate.saveOrUpdate(a);
	}
}
